<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="public/layui/css/layui.css">
    <script type="text/javascript" src="public/layui/layui.js"></script>
    <title>注册</title>
    <style>
        .header {
            background-color: #1e9fff;
            font-size: 12px;
            padding: 10px;
            color: #fff;
        }
    </style>
</head>

<body>

    <div class="header">
        <h1>注册</h1>
        <a href="?p=index" class="layui-font layui-font-12 " style="color: #fff;">
            <{$sys.base.title}>
        </a>
    </div>
    <form class="layui-form layui-padding-3">
        <div class="demo-login-container">
            <div class="layui-form-item">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-username"></i>
                    </div>
                    <input type="text" name="username" value="" lay-verify="required" placeholder="用户名"
                        lay-reqtext="请填写用户名" autocomplete="off" class="layui-input" lay-affix="clear">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-password"></i>
                    </div>
                    <input type="password" name="password" value="" lay-verify="required" placeholder="密   码"
                        lay-reqtext="请填写密码" autocomplete="off" class="layui-input" lay-affix="eye">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-email"></i>
                    </div>
                    <input type="email" name="email" value="" lay-verify="required" placeholder="邮  箱"
                        lay-reqtext="请填写邮箱" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row">
                    <div class="layui-col-xs7">
                        <div class="layui-input-wrap">
                            <div class="layui-input-prefix">
                                <i class="layui-icon layui-icon-vercode"></i>
                            </div>
                            <input type="number" name="code" value="" lay-verify="required" placeholder="验证码"
                                lay-reqtext="请填写验证码" autocomplete="off" class="layui-input" lay-affix="clear">
                        </div>
                    </div>
                    <div class="layui-col-xs5">
                        <div style="margin-left: 10px;">
                            <!-- <img id="captcha" src="?p=index&c=register&a=captcha" onclick="this.src='?p=index&c=register&a=captcha&m='+ new Date().getTime();"> -->
                            <button id="getCode" type="button" class="layui-btn layui-bg-blue">获取验证码</button>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <input type="checkbox" name="remember" lay-skin="primary" title="记住登录">
                <a href="#forget" style="float: right; margin-top: 7px;">忘记密码？</a>
            </div>
            <div class="layui-form-item">
                <button class="layui-btn layui-bg-blue layui-btn-fluid" lay-submit lay-filter="reg">注册</button>
            </div>
            <div class="layui-form-item">
                <a href="?p=index&c=login&a=index"
                    class="layui-btn layui-btn-primary layui-border-blue  layui-btn-fluid">登录</a>
            </div>
        </div>
    </form>
    <p style="text-align: center;">
        <a href="?p=index" class="layui-font-gray layui-font-12">
            <{$sys.base.copyright}>
        </a>
    </p>
    <script>

        layui.use(
            function () {
                $ = layui.$;
                var layer = layui.layer
                var form = layui.form
                form.on('submit(reg)', function (data) {
                    var field = data.field; // 获取表单字段值
                    $.post('?p=index&c=register&a=doreg', field, (res) => {
                        if (res.code == 200) {
                            layer.msg('注册成功')
                        } else if (res.code == 203) {
                            $('#captcha').attr('src', '?p=index&c=register&a=captcha&m=' + Math.random())
                            layer.msg(res.msg)
                        } else {
                            layer.msg(res.msg)
                        }
                    }, 'json')
                    return false; // 阻止默认 form 跳转
                });
                let waits = false
                $('#getCode').click(function (e) {
                    let t = 300
                    if (waits) {
                        layer.msg('请勿频繁发送验证码')
                        return false
                    }
                    getCode((res => {
                        if (res) {
                            waits = true
                            e.target.innerHTML = t
                            const n = setInterval(() => {
                                t -= 1
                                e.target.innerHTML = t
                                if (t == 0) {
                                    waits = false
                                    clearInterval(n)
                                    e.target.innerHTML = '获取验证码'
                                }
                            }, 1000);
                        }
                    }))
                })
                function getCode(cal) {
                    const em = $("input[name='email']").val()
                    var reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
                    if (!reg.test(em)) {
                        layer.msg('请输入正确的邮箱')
                        return false
                    }
                    $.post({
                        url: '?p=index&c=register&a=getCode',
                        data: {
                            email: em
                        },
                        success: (res) => {
                            if (res.code == 200) {
                                cal(true)
                                layer.msg('发送成功，请前往邮箱查看验证码')
                            } else {
                                layer.msg(res.msg)
                                cal(false)
                            }
                        }
                    }, 'json')
                }
            })

    </script>
</body>

</html>